package com.lanou.musicbox.fragment.onlinemusic;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.lanou.musicbox.R;
import com.lanou.musicbox.adapter.onlinemusic.EnterSingersListAdapter;
import com.lanou.musicbox.bean.EnterSingers;
import com.lanou.musicbox.custom.Events;
import com.lanou.musicbox.custom.NoScrollListView;
import com.lanou.musicbox.entity.Entity;
import com.lanou.musicbox.utils.NetWorkUtils;
import com.lanou.musicbox.utils.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;


/**
 * Created by 柳柏庆 on 15/10/29.
 */
public class SingersLeftFragment extends Fragment implements AdapterView.OnItemClickListener {
    private View view;//设置头布局
    private NoScrollListView singer_ListView;//定义listview
    private ArrayList<EnterSingers.DataEntity> dataEntities = new ArrayList<>();
    private EnterSingersListAdapter adapter;
    private VolleySingleton volleySingleton;
    private StringRequest request;
    private String songs_id;
    private TextView numlist;
    private String song_link,bg_img;
    private boolean isConnect;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.singers_songs_list, null);
        singer_ListView = (NoScrollListView) view.findViewById(R.id.noscroll_ListView);
        singer_ListView.setCacheColorHint(1);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        singer_ListView.setOnItemClickListener(this);
        volleySingleton = VolleySingleton.getInstance(getActivity());
        //通过bundle接受传过来的值
        Bundle bundle = getArguments();
        songs_id = bundle.getString("url");
        bg_img = bundle.getString("img");

        isConnect = NetWorkUtils.checkNet(getActivity());



        Log.i("lanou565656", "onActivityCreated " + songs_id);
        initdata();
    }
/**
 * 解析的构造方法
 * **/
    private void initdata() {
        request = new StringRequest(songs_id, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                EnterSingers enterSingers = gson.fromJson(response, EnterSingers.class);
                if (enterSingers != null && enterSingers.getData() != null) {
                    dataEntities = (ArrayList<EnterSingers.DataEntity>) enterSingers.getData();
                }
                adapter = new EnterSingersListAdapter(getActivity(), dataEntities);
                //initView();
                singer_ListView.setAdapter(adapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (isConnect){
                    Toast.makeText(getActivity(),"接口出错",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(getActivity(), R.string.connect_error, Toast.LENGTH_SHORT).show();
                }
            }
        });
        volleySingleton.addQueue(request);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
               List<Entity> songsInfos = new ArrayList<>();
        if (dataEntities.get(position).getAuditionList()!=null
                &&dataEntities.get(position).getAuditionList().size()>0)
        {
            for (int i = 0; i < dataEntities.size(); i++) {
                Entity netSongs = new Entity();

                if (dataEntities.get(i).getAuditionList()!=null&&dataEntities.get(i).getAuditionList().size()>0){
                    song_link = dataEntities.get(i).getAuditionList().get(1).getUrl();
                }
                else {
                    song_link = "";
                }
                String title = dataEntities.get(i).getName();
                String author = dataEntities.get(i).getSingerName();
                netSongs.setUrl(song_link);
                netSongs.setSmall_pic(bg_img);
                netSongs.setBig_pic(bg_img);
                netSongs.setTitle(title);

                netSongs.setArtist(author);
                netSongs.setItem_id(position);
                songsInfos.add(i, netSongs);
            }
            String whatMusic = "天天动听";
            Events.MusicInfoEvent musicInfoEvent= new Events.MusicInfoEvent();
            musicInfoEvent.setMp3Infos(songsInfos,whatMusic);
            EventBus.getDefault().post(musicInfoEvent);
        }
        else {
            Toast.makeText(getActivity(),"这首歌没有授权,我也无能为力哦!",Toast.LENGTH_SHORT).show();
        }



    }
}
